package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.OrderItem;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.RowMapper;

public class OrderItemDao {
	
	private DBHelp<OrderItem> db = new DBHelp<OrderItem>();
	
	public void save(OrderItem orderitem) {
		String sql = "INSERT INTO t_orderitem (orderid,prodid,num,total) VALUES (?,?,?,?)";
		db.executeSQL(sql, orderitem.getOrderid(),orderitem.getProdid(),orderitem.getNum(),orderitem.getTotal());
	}
	
	public List<OrderItem> findById(String id) {
		String sql = "SELECT orderid,prodid,num,total FROM t_orderitem WHERE orderid=?";
		return db.executeQueryForList(sql, new OrderItemRowMapper(), id);
	}
	
	public class OrderItemRowMapper implements RowMapper<OrderItem> {

		public OrderItem mapRow(ResultSet rs) throws SQLException {
			
			OrderItem item = new OrderItem();
			item.setNum(rs.getInt("num"));
			item.setOrderid(rs.getString("orderid"));
			item.setProdid(rs.getInt("prodid"));
			item.setTotal(rs.getFloat("total"));
			return item;
		}
	}
}
